<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
function cutAndPaste(selection)
{
    selection.document.execCommand('copy');
    selection.document.execCommand('delete');
    selection.document.execCommand('pasteAndMatchStyle');
}

test(() => {
    assert_not_equals(window.internals, undefined,
        'This test requires window.internals to access clipboard');

    assert_selection(
        '<div contenteditable>.^ foo|</div>',
        cutAndPaste,
        '<div contenteditable>.\u{00A0}foo|</div>',
        'Paste with leading space.');

    assert_selection(
        '<div contenteditable>^foo |.</div>',
        cutAndPaste,
        '<div contenteditable>foo\u{00A0}|.</div>',
        'Paste with trailing space.');

    assert_selection(
        '<div contenteditable>|x y^</div>',
        cutAndPaste,
        '<div contenteditable>x y|</div>',
        'One space');

    assert_selection(
        '<div contenteditable>|x\u{00A0} y^</div>',
        cutAndPaste,
        '<div contenteditable>x\u{00A0} y|</div>',
        'Two spaces');

    assert_selection(
        '<div contenteditable>|x\u{00A0} \u{00A0}y^</div>',
        cutAndPaste,
        '<div contenteditable>x\u{00A0} \u{00A0}y|</div>',
        'Three spaces');

    assert_selection(
        '<div contenteditable>|x\u{00A0} \u{00A0} y^</div>',
        cutAndPaste,
        '<div contenteditable>x\u{00A0} \u{00A0} y|</div>',
        'Four spaces');
}, 'paste with spaces:');
</script>
